

package com.teaching.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.common.Result;

import com.teaching.dto.UserDTO;
import com.teaching.entity.User;
import com.teaching.mapper.UserMapper;
import com.teaching.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author xubb
 * @date 2021-03-22 15:19:42
 */
@RestController
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理")
public class UserController {

    @Resource
    private  UserService userService;

    @Resource
    private UserMapper userMapper;

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/getUserList")
    public Result<IPage<User>> getUserList(@RequestBody UserDTO dto) {
        Page<User> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        User user = new User();
        BeanUtil.copyProperties(dto,user);
        return Result.success(userMapper.getUserList(page, user));
    }


    /**
     * 通过id查询
     *
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @PostMapping("/getUserInfo")
    public Result getUserInfo(@RequestBody User user) {
        return Result.success(userService.getById(user.getUserId()));
    }

    /**
     * 新增
     *
     * @param user
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody User user) {

        return Result.success(userService.save(user));
    }

    /**
     * 修改
     *
     * @param user
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody User user) {

        return Result.success(userService.updateById(user));
    }

    /**
     * 通过id删除
     *
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @PostMapping("/removeById")
    public Result removeById(@RequestBody User user) {
        return Result.success(userService.removeById(user.getUserId()));
    }

}
